import { fromJS } from 'immutable'
import moment from 'moment'

import { formatValue } from '../../utils/dataUtils'

import { APP_ACTION } from '../reducers/appReducer'
import { TRADE_MARKET_ACTION } from '../reducers/tradeMarketReducer'
import { TRADE_BID_ASK_ACTION } from '../reducers/tradeBidAskReducer'
import { TRADE_DEAL_DETAIL_ACTION } from '../reducers/tradeDealDetailReducer'
import { TRADE_CHART_ACTION } from '../reducers/tradeChartReducer'

export function dispatchOpen () {
  return {
    type: APP_ACTION,
    data: {
      wsConnectSuccess: true
    }
  }
}

export function dispatchClose () {
  return {
    type: APP_ACTION,
    data: {
      wsConnectSuccess: false
    }
  }
}

export function dispatchMessage (message) {
  return (dispatch, getState) => {
    const result = message.result || []
    // const currentContract = getState().trade.market.get('currentContract')
    // if (formatValue(currentContract, 'InstrumentID') !== data.InstrumentID) {
    //   return
    // }
    for (const data of result) {
      const type = data.table || ''
      switch (type) {
        case 'MarketTrade': {
          let dealDetail = getState().trade.dealDetail.get('dealDetail')
          dealDetail = dealDetail.unshift(fromJS(data.data))
          if (dealDetail.size > 200) {
            dealDetail = dealDetail.splice(200, dealDetail.size - 200)
          }
          dispatch({
            type: TRADE_DEAL_DETAIL_ACTION,
            data: {
              dealDetail
            }
          })
          break
        }
        case 'MarketOrder': {
          const bidAskData = data.data
          if (bidAskData.Direction === 49) {
            let totalAsk = getState().trade.bidAsk.get('totalAsk')
            if (bidAskData.Volume === 0) {
              totalAsk = totalAsk.delete(`ask_price_${bidAskData.Price}`)
            } else {
              totalAsk = totalAsk.set(`ask_price_${bidAskData.Price}`, fromJS(bidAskData))
            }
            dispatch({
              type: TRADE_BID_ASK_ACTION,
              data: {
                totalAsk
              }
            })
          } else {
            let totalBid = getState().trade.bidAsk.get('totalBid')
            if (bidAskData.Volume === 0) {
              totalBid = totalBid.delete(`bid_price_${bidAskData.Price}`)
            } else {
              totalBid = totalBid.set(`bid_price_${bidAskData.Price}`, fromJS(bidAskData))
            }
            dispatch({
              type: TRADE_BID_ASK_ACTION,
              data: {
                totalBid
              }
            })
          }
          break
        }
        case 'MarketData': {
          const market = data.data
          const open = +market.OpenPrice
          if (open || open === 0) {
            market.Change = (+market.LastPrice - open) / open * 100
          }
          dispatch({
            type: TRADE_MARKET_ACTION,
            data: {
              market: fromJS(market)
            }
          })
          break
        }
      }
    }
    // if (type.match('CKLineField')) {
    //   const currentContract = getState().trade.market.get('currentContract')
    //   const isInverse = formatValue(currentContract, 'IsInverse')
    //   const timestamp = moment(`${data.BeginDate} ${data.BeginTime}`, 'YYYY-MM-DD hh:mm:ss').valueOf()
    //   const kLineData = {
    //     time: timestamp, close: +data.ClosePrice,
    //     open: +data.OpenPrice, high: +data.HighestPrice,
    //     low: +data.LowestPrice, volume: +data.Volume,
    //     turnover: isInverse ? (+data.Volume) / (+data.Turnover) * (+data.Volume) : +data.Turnover,
    //     timestamp
    //   }
    //   dispatch({
    //     type: TRADE_CHART_ACTION,
    //     data: {
    //       kLineData: fromJS(kLineData)
    //     }
    //   })
    // }
  }
}
